package com.laiat.yamba;

import java.util.List;

import android.app.ActivityManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

import com.laiat.yamba.activity.StatusActivity;
import com.laiat.yamba.activity.TimelineActivity;

public class UpdaterService extends IntentService {
	private static final String TAG = UpdaterService.class.getSimpleName();

	public static final String BROADCAST_UPDATE_TIMELINE_ACTION_NAME = "com.laiat.yamba.intent.BroadcastTimelineRefresh";
	
	public static final String NEW_STATUS_EXTRA_COUNT = "com.laiat.yamba.intent.NewStatusExtraCount";

	private NotificationManager notificationManager;
	private Notification notification;
	
	public UpdaterService() {
		super(TAG);
		
		Log.d(TAG, "UpdaterService constructed");
		
	}
	
	@Override
	protected void onHandleIntent(Intent inIntent) {
		Log.d(TAG, "onHandleIntent'ing");
		
		notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		notification = new Notification(android.R.drawable.stat_notify_chat, "", 0);
		
		YambaApplication yamba = (YambaApplication)getApplication();
		int newUpdates = yamba.fetchStatusUpdates();
		if (newUpdates > 0) {
			Log.d(TAG, "We have a new status");
			
			//fire off broadcast to refresh timeline view
			Intent intent = new Intent();
			intent.setAction(BROADCAST_UPDATE_TIMELINE_ACTION_NAME);
			intent.putExtra(NEW_STATUS_EXTRA_COUNT, newUpdates);
			
			sendBroadcast(intent, Manifest.permission.RECEIVE_TIMELINE_NOTIFICATIONS);
			
			if (shouldSendTimelineNotification()) {
				sendTimelineNotification(newUpdates);
			}
		}
	}
	

	private boolean shouldSendTimelineNotification() {
		ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
		
		//only sent timeline notification if not in StatusActivity and not in TimelineActivity
		List<ActivityManager.RunningTaskInfo> taskList = activityManager.getRunningTasks(1);
		if (taskList.size() > 0) {
			ActivityManager.RunningTaskInfo task = taskList.get(0);

			String topActivityClassName = task.topActivity.getClassName();
			String[] activityNames = { StatusActivity.class.getName(), TimelineActivity.class.getName() };
			for(int i = 0; i < activityNames.length; i++) {
				if (topActivityClassName.equals(activityNames[i])) {
					return false;
				}
			}
		}
				
		return true;
	}
	
	/**
	 * Creates a notification in the notification bar telling user there are new messages
	 * 
	 * @param timelineUpdateCount Number of new statuses
	 */
	private void sendTimelineNotification(int timelineUpdateCount) {
		Log.d(TAG, "sendTimelineNotification'ing");
		
		PendingIntent pendingIntent = PendingIntent.getActivity(this, -1, new Intent(this, TimelineActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
		notification.when = System.currentTimeMillis();
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		
		CharSequence notificationTitle = getString(R.string.msgNotificationTitle);
		CharSequence notificationSummary = getString(R.string.msgNotificationMessage, timelineUpdateCount);
		notification.setLatestEventInfo(this, notificationTitle, notificationSummary, pendingIntent);
		
		notificationManager.notify(0, notification);
		
		Log.d(TAG, "sendTimelineNotification done");
	}

}
